import Mock from 'mockjs'
import { useUserListStore } from '@/stores/user'
import { useUserStore } from '@/stores/user'



Mock.mock('/user/login', 'post', (config) => {
    const user = useUserStore()
    const userList = useUserListStore()
    const req = JSON.parse(config.body)
    const data = {
        code: '200',
        message: '手机号不存在',
        data: {
            user: '',
            flag: false,
            exits: false
        }
    }
    let flag = false
    let exits = false
    userList.userList.forEach(item => {
        if (item.tel == req.tel) {
            exits = true
        }
        if (item.tel == req.tel && item.password == req.password) {
            flag = true
            user.changeUserInfo(item)
        }
    })
    if (flag) {
        data.message = '登录成功'
        data.data = {
            user: user.userInfo,
            flag: flag,
            exits: exits,
        }
    } else if (exits) {
        data.message = '密码错误'
        data.data = {
            user: '',
            flag: flag,
            exits: exits,
        }
    }

    return data
})

Mock.mock('/user/logout', 'post', (config) => {
    // const req = JSON.parse(config.body)
    const user = useUserStore()
    user.clear()

    return {
        code: '200',
        message: '退出成功',
    }
})